package cn.hellochaos.generator.controller.web;


import cn.hellochaos.generator.entity.CaipuList;
import cn.hellochaos.generator.entity.User;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import cn.hellochaos.generator.entity.dto.ResultBean;
import cn.hellochaos.generator.service.UserCookbookService;
import cn.hellochaos.generator.entity.UserCookbook;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author test
 * @since 2021-01-23
 * @version v1.0
 */
@RestController
@RequestMapping("/user-cookbook")
public class UserCookbookController {

    @Autowired
    private UserCookbookService userCookbookService;

    /**
     * 收藏（动词）
     * TODO
     */
    @RequestMapping("favorite")
    public ResultBean<?> favorite(String uid,String cid) {
        return new ResultBean<UserCookbook>(userCookbookService.favorite(uid,cid));
    }

    /**
     * 获取收藏信息 (如果收藏就返回1，未收藏就返回0)
     * TODO*/
    @RequestMapping("is-favorite")
    public ResultBean<?> is_favorite(String uid,String cid) {
        System.out.println("开始判断该用户是否收藏菜谱");
        return new ResultBean<Boolean>(userCookbookService.isFavorite(uid,cid));
    }

    /**
     * 收藏（动词）
     * TODO
     */
    @RequestMapping(value = "getUserAllFavorite",method = RequestMethod.POST)
    public ResultBean<?> getUserAllFavorite(@RequestBody User user) {
        return new ResultBean<List<CaipuList>>(userCookbookService.getUserAllFavorite(user));
    }


    /*============================================*/
    /**
    * 查询分页数据
    */
    @RequestMapping(method = RequestMethod.GET)
    public ResultBean<?> listByPage(@RequestParam(name = "page", defaultValue = "1") int page,
                                    @RequestParam(name = "pageSize", defaultValue = "10") int pageSize,
                                    @RequestParam(name = "factor", defaultValue = "") String factor) {
        return new ResultBean<>(userCookbookService.listUserCookbooksByPage(page, pageSize,factor));
    }


    /**
    * 根据id查询
    */
    @RequestMapping(method = RequestMethod.GET, value = "/{id}")
    public ResultBean<?> getById(@PathVariable("id") Integer id) {
        return new ResultBean<>(userCookbookService.getUserCookbookById(id));
    }

    /**
    * 新增
    */
    @RequestMapping(method = RequestMethod.POST)
    public ResultBean<?> insert(@RequestBody UserCookbook userCookbook) {
        return new ResultBean<>(userCookbookService.insertUserCookbook(userCookbook));
    }

    /**
    * 删除
    */
    @RequestMapping(method = RequestMethod.DELETE, value = "/{id}")
    public ResultBean<?> deleteById(@PathVariable("id") Integer id) {
        return new ResultBean<>(userCookbookService.deleteUserCookbookById(id));
    }

    /**
    * 修改
    */
    @RequestMapping(method = RequestMethod.PUT)
    public ResultBean<?> updateById(@RequestBody UserCookbook userCookbook) {
        return new ResultBean<>(userCookbookService.updateUserCookbook(userCookbook));
    }
}
